package com.xbongbong.paas.toolbox.util;

/**
 * @author sunshaoxiang
 * @ClassName TelephoneMaskUtil
 * @Description 电话掩码工具类
 * a. 显示规则：1-3位显示原值，4-7位星号显示，第7位及以上显示原值。如“110”、“100**”、“673****8”、“158****8821”
 * @Date 2022/4/1 15:33
 * @Version 1.0
 **/
public class TelephoneMaskUtil {

    public static String maskTelephone(String telephoneNumber) {
        if (StringUtil.isEmpty(telephoneNumber)) {
            return "";
        } else {
            StringBuilder telephoneCode = new StringBuilder(telephoneNumber);
            if (telephoneNumber.length() <= 3) {
                return telephoneNumber;
            } else if (telephoneNumber.length() <= 7) {
                telephoneCode.replace(3, telephoneNumber.length(), getMaskCode(telephoneNumber));
                return telephoneCode.toString();
            } else {
                telephoneCode.replace(3, 7, "****");
                return telephoneCode.toString();
            }
        }
    }

    /**
     * 返回掩码 "*"
     * @param telephoneNumber
     * @return
     */
    private static String getMaskCode (String telephoneNumber) {
        int codeNum = telephoneNumber.length() - 3;
        StringBuilder maskCode = new StringBuilder("");
        for (int i = 0; i < codeNum; i++) {
            maskCode.append("*");
        }
        return  maskCode.toString();
    }
}
